package tools;

import org.springframework.util.StringUtils;

import java.io.*;
import java.sql.SQLException;
import java.util.*;

public class AnalysisRegionData {

    public static void main(String[] args) {
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(new FileInputStream("C:\\Users\\Administrator\\Desktop\\gs-messaging-redis-master" +
                        "\\4test\\src\\main\\resources\\mmregioncode_zh_CN.txt")));
             BufferedReader enReader = new BufferedReader(
                     new InputStreamReader(new FileInputStream("C:\\Users\\Administrator\\Desktop\\gs-messaging-redis-master" +
                             "\\4test\\src\\main\\resources\\mmregioncode_en.txt")));
             BufferedReader twReader = new BufferedReader(
                     new InputStreamReader(new FileInputStream("C:\\Users\\Administrator\\Desktop\\gs-messaging-redis-master" +
                             "\\4test\\src\\main\\resources\\mmregioncode_zh_TW.txt")))) {

            String line = "";
            line = reader.readLine();
            List<RegionData> list = new ArrayList<>();
            while (line != null) {
                line = reader.readLine(); // 一次读入一行数据
                if (!StringUtils.isEmpty(line) &&( line.startsWith("CN") || line.startsWith("HK") || line.startsWith("TW") || line.startsWith("MO"))) {
                    String[] datas = line.split("\\|");
                    list.add(new RegionData(datas[0], datas[1], datas[0].split("_").length));
                }
            }

            List<RegionData> enList = new ArrayList<>();
            line = enReader.readLine();
            while (line != null) {
                line = enReader.readLine(); // 一次读入一行数据
                if (!StringUtils.isEmpty(line) &&( line.startsWith("CN") || line.startsWith("HK") || line.startsWith("TW") || line.startsWith("MO"))) {
                    String[] datas = line.split("\\|");
                    enList.add(new RegionData(datas[0], datas[1]));
                }
            }

            List<RegionData> twList = new ArrayList<>();
            line = twReader.readLine();
            while (line != null) {
                line = twReader.readLine(); // 一次读入一行数据
                if (!StringUtils.isEmpty(line) &&( line.startsWith("CN") || line.startsWith("HK") || line.startsWith("TW") || line.startsWith("MO"))) {
                    String[] datas = line.split("\\|");
                    twList.add(new RegionData(datas[0], datas[1]));
                }
            }

            enList.sort(Comparator.comparing(RegionData::getId));
            list.sort(Comparator.comparing(RegionData::getId));
            twList.sort(Comparator.comparing(RegionData::getId));

            Iterator<RegionData> iterable = enList.iterator();
            while (iterable.hasNext()) {
                for (RegionData regionData : list) {
                    RegionData tmp = iterable.next();
                    if (regionData.id.equals(tmp.id)) {
                        regionData.setEnglishName(tmp.englishName);
                    }
                }
            }

            iterable = twList.iterator();
            while (iterable.hasNext()) {
                for (RegionData regionData : list) {
                    RegionData tmp = iterable.next();
                    if (regionData.id.equals(tmp.id)) {
                        regionData.setTraditionName(tmp.englishName);
                    }
                }
            }


            iterable = list.iterator();
            List<Map<String,Object>> data = new ArrayList<>();
            while (iterable.hasNext()) {
                RegionData tmp = iterable.next();
                data.add(new LinkedHashMap(){
                    {
                        put("id", tmp.id);
                        put("name", tmp.name);
                        put("tradition_name", tmp.traditionName);
                        put("english_name", tmp.englishName);
                        put("type", tmp.type);
                    }
                });
            }

            try {
                int count = JdbcUtil.batchInsert("wechat_region",Arrays.asList("id", "name",
                                    "tradition_name","english_name","type"),
                                    data);
            } catch (SQLException e) {
                e.printStackTrace();
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            e.printStackTrace();
        }


    }


    static class RegionData {
        private String id;
        private String name;
        private String traditionName;
        private String englishName;
        private int type;

        public RegionData(String id, String name, int type) {
            this.id = id;
            this.name = name;
            this.type = type;
        }

        public RegionData(String id, String englishName) {
            this.id = id;
            this.englishName = englishName;
        }

        public String getEnglishName() {
            return englishName;
        }

        public void setEnglishName(String englishName) {
            this.englishName = englishName;
        }

        public void setTraditionName(String traditionName) {
            this.traditionName = traditionName;
        }

        public String getId() {
            return id;
        }

        @Override
        public String toString() {
            return "RegionData{" +
                    "id='" + id + '\'' +
                    ", name='" + name + '\'' +
                    ", englishName='" + englishName + '\'' +
                    ", type=" + type +
                    '}';
        }
    }

}
